package com.telecom.weixi;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.telecom.weixi.parse.ServBaseInfo2;
import com.telecom.weixi.view.SearchBlockView;
import com.telecom.weixi.view.SearchBlockView.OnBlockTitleListenner;
import com.telecom.weixi.view.TitleView;

public class SearchBoardActivity extends BaseActivity implements OnBlockTitleListenner {
	public static final String Tag_Search_Info = "search_info";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_board);

		((TitleView) findViewById(R.id.title_view)).setPreButtonListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		getSearchBlockView();

		getData();
	}

	List<ServBaseInfo2> mServBaseInfo;

	void getData() {
		Bundle data = getIntent().getExtras();
		if (data != null) {
			mServBaseInfo = data.getParcelableArrayList(Tag_Search_Info);
			if (mServBaseInfo != null) {
				updateUserList(mServBaseInfo);
			}
		}
	}

	static final int PageSize = 3;

	void updateUserList(List<ServBaseInfo2> infos) {
		int num = infos.size();
		for (int index = 0; index < num; index++) {
			ServBaseInfo2 baseInfo = infos.get(index);

			// Log.e(this.getClass().getName(), baseInfo.toString());

			SearchBlockView view = new SearchBlockView(this);
			view.setRowNum(index);
			view.setTitleClickListener(this);

			view.initTitle(baseInfo.getAccNbr(), baseInfo.getTraitName(), baseInfo.getCustType(), baseInfo.getBrandName());
			view.init1Row(baseInfo.getServName(), baseInfo.getJoinDate(), baseInfo.getState());
			view.initLatn_name(baseInfo.getLatn_name());
			view.add2Row(baseInfo.getManager(), baseInfo.getArpu());
			view.add2Row(baseInfo.getMonFlow(), baseInfo.getLastFlow());
			view.add2Row(baseInfo.getAcctNum(), baseInfo.getPayBal());

			LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			lp.setMargins(4, 0, 4, 2);
			container.addView(view, lp);
		}

		if (num == 1 || num > PageSize)
			return;
		for (int index = num; index < PageSize; index++) {
			SearchBlockView view = new SearchBlockView(this);
			view.initTitle("", "", "", "");
			view.init1Row("", "", "");
			view.add2Row("", "");
			view.add2Row("", "");
			view.add2Row("", "");

			LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			lp.setMargins(4, 4, 4, 4);
			container.addView(view, lp);
		}
	}

	LinearLayout container;

	void getSearchBlockView() {
		container = (LinearLayout) findViewById(R.id.container);
	}

	@Override
	public void onBlockTitleClick(int pos) {
		ServBaseInfo2 item = mServBaseInfo.get(pos);
		Intent intent = new Intent(SearchBoardActivity.this, New1Activity.class);
		intent.putExtra(New1Activity.TAG_SERV_ID, item.getServId());
		intent.putExtra(New1Activity.TAG_CUST_ID, item.getCustId());
		intent.putExtra(New1Activity.TAG_CUST_NBR, item.getAccNbr());
		startActivityForResult(intent, WeixiApplication.CODE_REQ);
	}

}
